package org.gp.Interfaz;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import org.gp.tringes.GestorIGGestionarCarreras;
import org.gp.tringes.GestorIGGestionarModalidadesTrineo;
import org.gp.tringes.GestorIGGestionarResultadosCarreras;
import org.gp.tringes.GestorIGInicio;

import java.awt.FlowLayout;


public class IGAnadirModalidad extends JDialog {
	
	private String descripcionVieja;
	
	private JLabel lblDescrip;
	private JTextField txtDescrip;
	private JButton btnAnadir;
	private JButton btnCancelar;
	private JLabel lblNumMP;
	private JTextField textNumMaxP;

	
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel_4;

	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IGAnadirModalidad dialog = new IGAnadirModalidad();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
					dialog.setModal(true);
					dialog.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public IGAnadirModalidad() {
					
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
		//ID-251
		setTitle(GestorIGInicio.getGestorIGInicio().getTexto(251));
		setBounds(100, 100, 385, 161);
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.CENTER);
		getContentPane().add(getPanel_1(), BorderLayout.SOUTH);
		getContentPane().add(getPanel_2(), BorderLayout.NORTH);

		
	}
	
	/*****************
	 * Labels
	 ***************/

	private JLabel getLblDescrip(){
		if(lblDescrip == null){
			//ID-245
			lblDescrip = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(245));
			lblDescrip.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblDescrip;
	}
	
	
	private JLabel getLblNumMP() {
		if (lblNumMP == null) {
			//ID-246
			lblNumMP = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(246));
			lblNumMP.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNumMP;
	}
	
	/*****************
	 * Cajas de Texto
	 ***************/
	
	private JTextField getTxtDescrip(){
		if(txtDescrip == null){
			txtDescrip = new JTextField();
			txtDescrip.setColumns(10);
		}
		
		//Le ponemos el nombre que tenia anteriormente
		return txtDescrip;
	}

	private JTextField getTextNumMaxP() {
		if (textNumMaxP == null) {
			textNumMaxP = new JTextField();
			textNumMaxP.setColumns(10);
		}
		return textNumMaxP;
	}

	
	/*****************
	 * Botones
	 ***************/
	
	private JButton getBtnAnadir(){
		if(btnAnadir == null){
			//ID-250
			btnAnadir = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(250));
			btnAnadir.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					String pModalidad=txtDescrip.getText();
					String pNumMaxP=textNumMaxP.getText();
					boolean esNumMaxPCorrecto=false;
					
					//Comprobamos que los datos introducidos son correctos
					//Comprobamos que la modalidad tiene la longitud apropiada
					boolean esCorrecto = GestorIGGestionarCarreras.getGestorIGGestionarCarreras().comprobarLongitud(pModalidad,20);
					//Comprobamos que el numMaxPerros es un numero
					int numMPerros = GestorIGGestionarResultadosCarreras.getGestorIGGestionarResultadosCarreras().comprobarEsNum(pNumMaxP);
					if(numMPerros!=-1 && numMPerros>0){
						esNumMaxPCorrecto=true;
					}
					
					if(!esCorrecto){
						//ID-247 y 4
						JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(247), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
					}else if(!esNumMaxPCorrecto){
						//ID-248 y 4
						JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(248), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
					}else{
						GestorIGGestionarModalidadesTrineo.getGestorIGGestionarModalidadesTrineo().anadirModalidad(pModalidad, pNumMaxP);
						//ID-252 y 6
						JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(252), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.INFORMATION_MESSAGE);
						dispose();
						}	
				}
			});
		}
		return btnAnadir;
	}
	private JButton getBtnCancelar(){
		if(btnCancelar == null){
			//ID-8
			btnCancelar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(8));
			btnCancelar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					dispose();
				}
			});
		}
		return btnCancelar;
	}
	

	
	
	
	/*****************
	 * Paneles
	 ***************/
	
	private JPanel getPanel(){
		if(panel == null){
			panel = new JPanel();
			panel.setLayout(new GridLayout(0, 1, 0, 15));
			panel.add(getPanel_4());
		}
		return panel;
	}
	private JPanel getPanel_1(){
		if(panel_1 == null){
			panel_1 = new JPanel();
			FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
			flowLayout.setVgap(10);
			panel_1.add(getBtnAnadir()); 
			panel_1.add(getBtnCancelar());
		}
		return panel_1;
	}
	private JPanel getPanel_2(){
		if(panel_2 == null){
			panel_2 = new JPanel();
		}
		return panel_2;
	}
	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.setLayout(new GridLayout(2, 2, 0, 15));
			panel_4.add(getLblDescrip());
			panel_4.add(getTxtDescrip());
			panel_4.add(getLblNumMP());
			panel_4.add(getTextNumMaxP());
		}
		return panel_4;
	}


}
